package com.zzp.zjs_bos.web.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletOutputStream;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.zzp.zjs_bos.domain.Region;
import com.zzp.zjs_bos.domain.Subarea;
import com.zzp.zjs_bos.utils.FileUtils;
import com.zzp.zjs_bos.web.action.base.BaseAction;


@Controller
@Scope("prototype")
public class SubareaAction extends BaseAction<Subarea> {
	
	public String save(){
		subareaService.save(model);
		return "list";
	}
	
	public String pageQuery(){
		
		String addresskey = model.getAddresskey();
		Region region = model.getRegion();
		DetachedCriteria subareaDC = pageBean.getDetachedCriteria();

		if(StringUtils.isNotBlank(addresskey)){
			subareaDC.add(Restrictions.like("addresskey","%"+addresskey+"%" ));
		}
		
		if(region != null){
			DetachedCriteria regionDC = subareaDC.createCriteria("region");
			String province = region.getProvince();
			String city = region.getCity();
			String district = region.getDistrict();
			if(StringUtils.isNotBlank(province)){
				regionDC.add(Restrictions.like("province","%"+province+"%" ));
			}
			if(StringUtils.isNotBlank(district)){
				regionDC.add(Restrictions.like("district", "%"+district+"%"));
			}
			if(StringUtils.isNotBlank(city)){
				regionDC.add(Restrictions.like("city", "%"+city+"%"));
			}
			
		}
		
		subareaService.pageQuery(pageBean);
		String[] excludes = new String[]{"subareas","decidedzone"};
		this.writePageBean2Json(pageBean, excludes);
		return NONE;
	}
	
	public String exportXls() throws IOException{
		List<Subarea> list = subareaService.findAll();
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet("分区数据");
		HSSFRow headRow = sheet.createRow(0);
		headRow.createCell(0).setCellValue("分区编号");
		headRow.createCell(1).setCellValue("区域编码");
		headRow.createCell(2).setCellValue("关键字");
		headRow.createCell(3).setCellValue("起始号");
		headRow.createCell(4).setCellValue("结束号");
		headRow.createCell(5).setCellValue("单双号");
		headRow.createCell(6).setCellValue("位置信息");
		
		for (Subarea subarea : list) {
			//每遍历一次就创建一行
			HSSFRow dateRow = sheet.createRow(sheet.getLastRowNum() + 1);
			dateRow.createCell(0).setCellValue(subarea.getId());
			dateRow.createCell(1).setCellValue(subarea.getRegion().getId());
			dateRow.createCell(2).setCellValue(subarea.getAddresskey());
			dateRow.createCell(3).setCellValue(subarea.getStartnum());
			dateRow.createCell(4).setCellValue(subarea.getEndnum());
			dateRow.createCell(5).setCellValue(subarea.getSingle());
			dateRow.createCell(6).setCellValue(subarea.getPosition());
		}
		
		//文件下载，设置2个头和一个流
		
		ServletOutputStream out = ServletActionContext.getResponse().getOutputStream();
		
		String fileName = "分区数据.xls";
		
		//解决中文不显示问题
		fileName = FileUtils.encodeDownloadFilename(fileName,ServletActionContext.getRequest().getHeader("user-agent"));
	
		//设置2个头
		ServletActionContext.getResponse().setContentType(ServletActionContext.getServletContext().getMimeType(fileName));
		
		ServletActionContext.getResponse().setHeader("content-disposition","attachment;filename=" + fileName);
		
		workbook.write(out);
		return NONE;
	}
	
	public String findSome(){
		DetachedCriteria dc = DetachedCriteria.forClass(Subarea.class);
		dc.add(Restrictions.isNull("decidedzone"));
		List<Subarea> list = subareaService.findSome(dc);
		String[] excludes = new String[]{"decidedzone","region","single","startnum","endnum","single"};
		this.writeList2Json(list, excludes);
		return NONE;
	}
}
